//
//  CategoryModel.m
//  Edu_iOS
//
//  Created by yanyan on 2017/9/8.
//  Copyright © 2017年 yanyan. All rights reserved.
//

#import "CategoryModel.h"
#import "CYDataHelper.h"
@implementation PrimaryCategoryModel
- (instancetype)initWithObject:(id)obj
{
    return [self initWithObject:obj hasAll:YES];
}
- (instancetype)initWithObject:(id)obj hasAll:(BOOL)flag
{
    if (self = [super init]) {
    self.title = [obj valueForKey:@"name"];
    self.modelId = [obj valueForKey:@"id"];
        if ([((NSDictionary *)obj).allKeys containsObject:@"icon"]) {
            if (![CYDataHelper isNull:[obj valueForKey:@"icon"]]) {
                self.iconURLString = [obj valueForKey:@"icon"];
            }
        }
        NSMutableArray *arr = [NSMutableArray array];

        if (flag) {
            if ([[obj valueForKey:@"children"] count] > 0) {
                PrimaryCategoryModel *mo = [[PrimaryCategoryModel alloc]init];
                mo.title = @"全部";
                mo.modelId = self.modelId;
                mo.subCategoryArray = [NSMutableArray array];
                [arr addObject:mo];
            }
        }
   
    for (id subObj in [obj valueForKey:@"children"]) {
        PrimaryCategoryModel * sub_mo= [[PrimaryCategoryModel alloc]initWithObject:subObj];
        [arr addObject:sub_mo];
    }
    self.subCategoryArray = arr;
}
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init])
    {
        _title = [aDecoder decodeObjectForKey:@"title"];
        _modelId = [aDecoder decodeObjectForKey:@"modelId"];
        _subCategoryArray = [aDecoder decodeObjectForKey:@"subCategoryArray"];
        _selected = [aDecoder decodeBoolForKey:@"selected"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:_title forKey:@"title"];
    [aCoder encodeObject:_modelId forKey:@"modelId"];
    [aCoder encodeObject:_subCategoryArray forKey:@"subCategoryArray"];
    [aCoder encodeBool:_selected forKey:@"selected"];
}

@end
@implementation SubCategoryModel


- (instancetype)initWithObject:(id)obj
{
    if (self = [super initWithObject:obj]) {
        
    }
    return self;
}

@end
@implementation CategoryModel

@end
